/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.config;

import com.zxp.springboot.constants.BeetlConstants;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.Properties;

/**
 * 源码见： com.ibeetl.starter.BeetlTemplateConfig
 * 参考作者项目 https://gitee.com/xiandafu/springboot-plus
 * @desc:
 * @author: shollin
 * @date: 2021/6/20/020 20:38
 */
@Configuration
public class BeetlConf {

    @Value("${spring.mvc.view.prefix:'/pages'}")
    private String prefix;

    @Value("${beetl.templatesPath:templates}")
    String templatesPath;// 模板跟目录

    @Value("${beetl.suffix:html}")
    String suffix;// 模板后缀

    @Value("${beetl-beetlsql.dev:true}")
    boolean dev;

    @Autowired
    Environment env;

    @Autowired
    ApplicationContext applicationContext;

// 覆盖掉官方starter里面的 BeetlGroupUtilConfiguration， com.ibeetl.starter.BeetlTemplateConfig
    //@Bean(initMethod = "init",name="beetlConfig")
    public CustomBeetlGroupUtilConfiguration customBeetlGroupUtilConfiguration() {
        CustomBeetlGroupUtilConfiguration customBeetlGroupUtilConfiguration = new CustomBeetlGroupUtilConfiguration(env);

        // 设置beetl的资源加载器
        customBeetlGroupUtilConfiguration.setResourceLoader(new ClasspathResourceLoader(BeetlConf.class.getClassLoader(), templatesPath));

        // 设置beetl的配置
        customBeetlGroupUtilConfiguration.setConfigProperties(createBeetlProperties());

        return customBeetlGroupUtilConfiguration;
    }

    /**
     * 组装beetl初始化需要的properties
     *
     * @author fengshuonan
     * @date 2020/12/27 12:33
     */
    private Properties createBeetlProperties() {
        Properties properties = new Properties();
        if(dev){
        }else{
            properties.put("RESOURCE.autoCheck", "false");
        }
        properties.put("RESOURCE.autoCheck", "true");
        // 定义边界符开始符号
        properties.setProperty("DELIMITER_STATEMENT_START", BeetlConstants.DEFAULT_DELIMITER_STATEMENT_START);
        // 定义边界符结束符号
        properties.setProperty("DELIMITER_STATEMENT_END",BeetlConstants.DEFAULT_DELIMITER_STATEMENT_END);
        // 默认beetl 支持HTML标签
        properties.setProperty("HTML_TAG_FLAG", BeetlConstants.DEFAULT_HTML_TAG_FLAG);
        // 自定义标签文件Root目录
        properties.setProperty("RESOURCE.tagRoot", BeetlConstants.DEFAULT_RESOURCE_TAG_ROOT);
        // 自定义标签文件后缀
        properties.setProperty("RESOURCE.tagSuffix", BeetlConstants.DEFAULT_RESOURCE_TAG_SUFFIX);
        //是否检测文件变化,开发用true合适，但线上要改为false
        //properties.setProperty("RESOURCE.autoCheck", BeetlConstants.DEFAULT_RESOURCE_AUTO_CHECK);
        return properties;
    }

}
